/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package gene.core.sound;

import java.io.File;
import java.io.IOException;

import java.io.InputStream;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.sound.midi.InvalidMidiDataException;
import javax.sound.midi.MidiSystem;
import javax.sound.midi.MidiUnavailableException;
import javax.sound.midi.Sequence;
import javax.sound.midi.Sequencer;
import javax.sound.midi.Synthesizer;
import javax.sound.midi.Receiver;
import javax.sound.midi.Transmitter;

public class SoundPlayer
{
	
	private  Sequencer	sm_sequencer = null;
	private  Synthesizer	sm_synthesizer = null;
    private  byte TYPE;
    File music;
     /**
     * constant SOUND PLAY
     */
    public static final int SOUND_PLAYING = 1;
    /**
     * constant SOUND STOP
     */
    public static final int SOUND_STOPPED = 2;
    /**
     * constant SOUND STOP
     */
    public static final int STATE_SOUND_OFF = 2;
    /**
     * constant SOUND PLAY
     */
    public static final int STATE_SOUND_ON = 1;

    private  final int PLAY=1,STOP=2;
    private  StringBuffer filename;
    private  InputStream in;
    private boolean loop,running=true;
    private   int STATE=2;
    private   int volume=100;
    //javazoom.jl.player.Player mp3Player;

    public SoundPlayer(){
     
    }


    /*
     public void run() {
        while(running){
       if(STATE!=PLAY){
        switch(TYPE)
        {
            case 1:{playMidi(filename, loop);break;}
            case 2:{playMp3(filename, loop);break;}
        }
        }
        if(TYPE==0 && STATE == STOP){//this.mp3Player.close();
                                     this.mp3Player=null;}
        try{t.sleep(150);}catch(Exception e){}
        }
    }*/
    
    public void destroy()
    {
      stopMusic();
 //     mp3Player=null;
      filename=null;
      music=null;
      sm_sequencer=null;
      sm_synthesizer=null;
      running=false;
    }

	public void playMusic(StringBuffer strFilename,boolean loop)
	{if(STATE == STOP){
         STATE = PLAY;
         Sequence	sequence=null;
		try{
            if(strFilename !=null){
            this.music= new File("./src/"+strFilename.toString());
            sequence = MidiSystem.getSequence(music);
            }
            else
            {
            sequence = MidiSystem.getSequence(in);
            }

        }//getSequence(this.music);}
		catch (InvalidMidiDataException e){e.printStackTrace();}
		catch (IOException e){e.printStackTrace();}
		try{	sm_sequencer = MidiSystem.getSequencer();}
		catch (MidiUnavailableException e){	e.printStackTrace();}
		if (sm_sequencer == null){}

		try{sm_sequencer.open();}
		catch (MidiUnavailableException e){e.printStackTrace();}
		try{sm_sequencer.setSequence(sequence);}
		catch (InvalidMidiDataException e){e.printStackTrace();}
		if (! (sm_sequencer instanceof Synthesizer)){
			try{
                sm_synthesizer = MidiSystem.getSynthesizer();
				sm_synthesizer.open();
                if(loop){
                sm_sequencer.setLoopCount(sm_sequencer.LOOP_CONTINUOUSLY);
                }
				Receiver	synthReceiver = sm_synthesizer.getReceiver();
				Transmitter	seqTransmitter = sm_sequencer.getTransmitter();
				seqTransmitter.setReceiver(synthReceiver);
			}
			catch (MidiUnavailableException e){e.printStackTrace();}
		}
		sm_sequencer.start();
     }//if
	}

public void stopMusic(){

                           if(STATE == PLAY)
                           {STATE=STOP;
                               if(sm_sequencer!=null){sm_sequencer.stop();}
                               if(sm_sequencer!=null){sm_sequencer.close();}
                               if(sm_synthesizer!=null){sm_synthesizer.close();}
                               if(sm_sequencer!=null){ sm_sequencer= null;}
                               if(sm_synthesizer!=null){sm_synthesizer = null;}
//                               if(mp3Player!=null){mp3Player.close();}
//                               if(mp3Player!=null){mp3Player=null;}
                              System.out.println("----------------------");
                            this.TYPE=0;
                        }
                   
}


private void playMp3( StringBuffer fileName,boolean loop) {
    if(STATE == STOP){
       STATE = PLAY;
        try {
            if(fileName == null){
            }else
            {this.music = new File(fileName.toString());
             in = (java.io.InputStream)(new java.io.FileInputStream(fileName.toString()));
            }
//            mp3Player= new javazoom.jl.player.Player(in);
//            if(loop){mp3Player.play(-1);}
//            else{mp3Player.play();}
        } catch (Exception ex) {
            Logger.getLogger(SoundPlayer.class.getName()).log(Level.SEVERE, null, ex);
        }
       fileName=null;
       
    }
   } // playAudioFile



public void setSoundState(int _state){
}

public int getSoundState(){
return this.STATE;
}



}
